﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;

namespace Dreamz
{
    public class Rum3
    {
        static public void Apply()
        {
           //Adds methods invoked through events, like taking cranium/holy hand grenade, using device, and looking at cobweb, also intializes the boolean CraniumFilled to false.
            GameEngine.S.World[4, 1].Items["cranium"].OnTake += new EventHandler(TakeCranium);
            GameEngine.S.World[4, 1].Items["holy hand grenade"].OnTake += new EventHandler(TakeGrenade);
            GameEngine.S.World[4, 1].Items["holy hand grenade"].OnUse += new EventHandler(UseGrenade); //granatmetoden
            GameEngine.S.World[4, 1].Items["device"].OnUse += new EventHandler(UseDevice);
            GameEngine.S.World[4, 1].Items["cobweb"].OnLook += new EventHandler(WebTrap);
            GameEngine.GameState.CraniumFilled = false;
        }
        static public void WebTrap()
        {
           //This gives the sad news that your character have died, and resets the game.
            UI.Console.Clear();
            UI.Console.Write("[GAME OVER]", ConsoleColor.Red);
            UI.Console.WriteLine(@"As you lean in to take a closer look at the cobweb, you get stuck in it's sticky threads.
Three days later, a giant spider creep down and devours you." + "\n");
            UI.Draw();
            Console.ReadKey();
            Console.Clear();
            GameEngine.S.Menu();
        }
        static public void UseDevice()
        {
           //Use the grammophone player
            UI.Console.Write("As you press some buttons on the device, it starts playing an odd tune, which sounds almost like a hymn.\n");
        }
        static public void TakeCranium()
        {
           //Adds a method invoked when using the cranium, as well as chaning the descriptions of all objects mentioning the cranium
            GameEngine.S.World[4, 1].Description = "This room contains a set of crown jewels, and an odd, noisy contraption.\n";
            GameEngine.S.Player.Inventory["cranium"].OnUse += new EventHandler(UseCranium);
        }
        static public void UseCranium()
        {
           //Invoked when you use the cranium, first checks if you are in the correct room, then asks you what you want to do with it two times.
           //the first time, the target should be liquid, to fill the cranium. This changes the relevant descriptions and switch a boolean.
           // The second time, it checks if you target a hole, and in that case fills it with the liquid, revealing a secret.
            string UseTarget;
            if (GameEngine.S.Player.X == 0 && GameEngine.S.Player.Y == 3)
            {
                UI.Console.Clear();
                UI.Console.Write("What do you want to use the Cranium on?\n");
                UI.Draw();
                UseTarget = Console.ReadLine().ToLower();
                if (UseTarget == "liquid" || UseTarget == "stream")
                {
                    GameEngine.S.Player.Inventory["cranium"].Description = "This is the cranium of a giant bull. It's filled with an un-identified liquid.\n";
                    GameEngine.GameState.CraniumFilled = true;
                    UI.Console.Write("You use the cranium as a bowl to hold the unidentified liquid.\n");
                }
                else
                {
                    UI.Console.Write("Stop this Gibberish!\n");
                }

            }
            else if (GameEngine.S.Player.X == 4 && GameEngine.S.Player.Y == 2)
            {
                if (GameEngine.GameState.CraniumFilled)
                {
                    UI.Console.Clear();
                    UI.Console.Write("What do you want to use the Cranium on?\n");
                    UI.Draw();
                    UseTarget = Console.ReadLine().ToLower();
                    if (UseTarget == "hole")
                    {
                        UI.Console.Write("You fill the hole with the un-identified liquid. An object float up to the surface.\n");
                        String item = "Ball|Wood|Ball-shaped piece of wood|Piece of wood|Piece#It's made out of wood, and round, duh#canPickUp";
                        GameEngine.S.World[4, 2].Items.Add(item.Split('|')[0].ToLower(), Item.FromString(item));
                        GameEngine.S.World[4, 2].Items["ball"].OnTake += new EventHandler(Rum6.TakeBall);
                        GameEngine.S.World[4, 2].Items["hole"].Description = "This is a hole filled with an unidentified liquid, it contains a ball-shaped piece of wood.\n";
                        GameEngine.S.Player.Inventory["cranium"].Description = "This is the cranium of a giant bull. It's slightly wet.\n";
                    }
                    else
                    {
                       UI.Console.Write("Stop this gibberish!\n");
                       UI.Draw();
                    }
                }
            }
            else
            {
                UI.Console.Write("Stop this gibberish!\n");
                UI.Draw();
            }


        }
        static public void TakeGrenade()
        {
           //Changes the description of jewels when you have the grenade
            GameEngine.S.World[4, 1].Items["crown jewels"].Description = "This is a set of crown jewels, you see a crown, a sceptre, but something seems to be missing...\n";
            GameEngine.S.Player.Inventory["holy hand grenade"].OnUse += new EventHandler(UseGrenade);
        }
        static public void UseGrenade() // granat funktionen startas
        {
            int seconds;
            if (GameEngine.S.Player.Inventory["holy hand grenade"] != null && GameEngine.S.Player.X == 1 && GameEngine.S.Player.Y == 3)  //granaten i inventory används
            {
                UI.Console.Clear();

                UI.Console.WriteLine("Where do you want to toss it?");    // riktlinjen vart du väljer att slänga granaten
                UI.Draw();
                string toss = Console.ReadLine().ToLower();                // ska läsas i småbokstäver
                if (toss == "south" || toss == "s" || toss == "go south")
                {
                    UI.Console.WriteLine("How many seconds do you want to wait before tossing it in?");
                    UI.Draw();
                    seconds = Time();
                    if (seconds > 3)
                    {
                        //This gives the sad news that your character have died, and resets the game.
                        UI.Console.Clear();
                        UI.Console.Write("[GAME OVER]", ConsoleColor.Red);
                        UI.Console.WriteLine("\nAs you throw the hand greande in, you realize you did a mistake.You hear a voice in your head, reciting. \"And the LORD spake, saying, \"First shalt thou take out the Holy Pin, then shalt thou count to three, no more, no less. Three shall be the number thou shalt count, and the number of the counting shall be three. Four shalt thou not count, neither count thou two, excepting that thou then proceed to three. Five is right out. Once the number three, being the third number, be reached, then lobbest thou thy Holy Hand Grenade of Antioch towards thy foe, who being naughty in My sight, shall snuff it.\" Amen.\"The bunny throws the hand grenade back, and you meet a fiery fate.");
                        UI.Draw();
                        Console.ReadKey();
                        Console.Clear();
                        GameEngine.S.Menu();
                    }
                    else if (seconds == 3)
                    {
                        GameEngine.S.Player.Inventory.Remove("holy hand grenade");
                        UI.Console.WriteLine("\nYou taketh out the holy pin, and then you hear a holy choir chant an odd hymn;\n\"Ha--le-lu-ja-, ha-le-lu-ja, haleluja... ha--le-lu-ja-, ha-le-e-lu-jah!\"\nInspired by this hymn, you slowly count to two, only to proceed to three.\nAs you count to exactly three, no more, no less, you throw the Holy Hand Grenade of Antioch.\nYou hear startled squeals as the grenade fly towards the horrid beast that, indeed,\nhave been naughty in the eyes of the LORD.\nThe cleansing explosion renders the room safe.");
                        UI.Draw();
                        GameEngine.GameState.IsMonsterDead = true;
                        Console.ReadKey();
                    }
                    else
                    {
                        //This gives the sad news that your character have died, and resets the game.
                        UI.Console.Clear();
                        UI.Console.Write("[GAME OVER]", ConsoleColor.Red);
                        UI.Console.WriteLine("As you throw the hand grenade in, you realize you did a mistake.\nYou hear a voice in your head, reciting. \n\"And the LORD spake, saying, \n\"First shalt thou take out the Holy Pin, then shalt thou count to three, no more, no less. Three shall be the number thou shalt count, and the number of the counting shall be three. Four shalt thou not count, neither count thou two, excepting that thou then proceed to three. \nFive is right out. Once the number three, being the third number, be reached, then lobbest thou thy Holy Hand Grenade of Antioch towards thy foe, who being naughty in My sight, shall snuff it.\n\" Amen.\"The Holy Hand Grenade of Antioch explodes in your hand, and you meet a fiery fate.");
                        UI.Draw();
                        Console.ReadKey();
                        Console.Clear();
                        GameEngine.S.Menu();
                    }
                }
                else
                {
                    // This gives the sad news that your character have died, and resets the game.
                    UI.Console.Clear();
                    UI.Console.Write("[GAME OVER]", ConsoleColor.Red);
                    UI.Console.WriteLine(@"You realise how stupid it is to pull the pin out of a grenade,
without knowing what to do with it, and stand there dumbfounded as you die.");
                    UI.Draw();
                    Console.ReadKey();
                    Console.Clear();
                    GameEngine.S.Menu();
                }
            }
            else
            {
                UI.Console.Clear();
                UI.Console.Write("[GAME OVER]", ConsoleColor.Red);
                UI.Console.WriteLine(@"You realise how stupid it is to pull the pin out of a grenade,
without knowing what to do with it, and stand there dumbfounded as you die.");
                UI.Draw();
                Console.ReadKey();
                Console.Clear();
                GameEngine.S.Menu();
            }
        }

        static public int Time()
        {
            int seconds;
            try
            {
                seconds = Convert.ToInt32(Console.ReadLine());
                return seconds;
            }
            catch (FormatException)
            {
                UI.Console.Write("Stop this gibberish and write an integer!\n");
                UI.Draw();
                seconds = Time();
                return seconds;
            }
        }
    }
}
